package com.wgc.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgc.goods.domain.GoodsBrand;
import com.wgc.goods.service.GoodsBrandService;
import com.wgc.goods.mapper.GoodsBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 15776
 * @description 针对表【goods_brand(品牌表)】的数据库操作Service实现
 * @createDate 2024-07-27 09:35:42
 */
@Service
public class GoodsBrandServiceImpl extends ServiceImpl<GoodsBrandMapper, GoodsBrand>
        implements GoodsBrandService {
    @Autowired
    private GoodsBrandMapper iBrandMapper;

    @Override
    public List<GoodsBrand> selectAll() {
        LambdaQueryWrapper<GoodsBrand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsBrand::getIsDelete, 0);
        return iBrandMapper.selectList(lqw);
    }

    @Override
    public Integer insert(GoodsBrand iBrand) {
        return iBrandMapper.insert(iBrand);
    }

    @Override
    public Integer update(GoodsBrand iBrand) {
        return iBrandMapper.updateById(iBrand);
    }

    @Override
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                GoodsBrand brand = new GoodsBrand();
                brand.setId(Long.parseLong(id));
                brand.setIsDelete(1);
                iBrandMapper.updateById(brand);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer status(Long id, Integer status) {
        GoodsBrand brand = new GoodsBrand();
        brand.setId(id);
        brand.setStatus(status);
        return iBrandMapper.updateById(brand);
    }

    @Override
    public Integer status(Long id) {
        GoodsBrand brand = iBrandMapper.selectById(id);
        if (brand.getStatus() == null) {
            return -1;
        }
        if (brand.getStatus() == 1) {
            brand.setStatus(0);
        } else {
            brand.setStatus(1);
        }
        return iBrandMapper.updateById(brand);

    }

    @Override
    public List<GoodsBrand> selectList(GoodsBrand iBrand) {
        LambdaQueryWrapper<GoodsBrand> lqw = this.lqw(iBrand);
        return iBrandMapper.selectList(lqw);
    }

    @Override
    public Page<GoodsBrand> findPage(Integer page, Integer size) {
        Page<GoodsBrand> page1 = new Page<>(page, size);
        LambdaQueryWrapper<GoodsBrand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsBrand::getIsDelete, 0);
        return this.page(page1,lqw);
    }

    @Override
    public Page<GoodsBrand> findPage(GoodsBrand iBrand, Integer page, Integer size) {
       Page<GoodsBrand> page1= new Page<>(page, size);
        return this.page(page1, lqw(iBrand));
    }

    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return iBrandMapper.findListByCategoryName(categoryName);
    }

    public LambdaQueryWrapper<GoodsBrand> lqw(GoodsBrand iBrand) {
        LambdaQueryWrapper<GoodsBrand> lqw = new LambdaQueryWrapper<>();
        if (iBrand.getId() != null) {
            lqw.eq(GoodsBrand::getId, iBrand.getId());
        }
        if (iBrand.getBrandName() != null) {
            lqw.eq(GoodsBrand::getBrandName, iBrand.getBrandName());
        }
        if (iBrand.getBrandLogo() != null) {
            lqw.eq(GoodsBrand::getBrandLogo, iBrand.getBrandLogo());
        }
        if (iBrand.getStatus() != null) {
            lqw.eq(GoodsBrand::getStatus, iBrand.getStatus());
        }
        if (iBrand.getIsDelete() != null) {
            lqw.eq(GoodsBrand::getIsDelete, iBrand.getIsDelete());
        }
        if (iBrand.getCreateTime() != null) {
            lqw.eq(GoodsBrand::getCreateTime, iBrand.getCreateTime());
        }
        if (iBrand.getUpdateTime() != null) {
            lqw.eq(GoodsBrand::getUpdateTime, iBrand.getUpdateTime());
        }
        return lqw;
    }

}




